package vip.gadfly.tiktok.mini.api.impl;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import vip.gadfly.tiktok.config.TtOpConfigStorage;
import vip.gadfly.tiktok.mini.api.TtMiniUserInfoService;
import vip.gadfly.tiktok.mini.bean.oauth2.TtMiniAccessTokenRequest;
import vip.gadfly.tiktok.mini.bean.oauth2.TtMiniAccessTokenResult;
import vip.gadfly.tiktok.mini.bean.userinfo.TtMiniFansListResult;
import vip.gadfly.tiktok.mini.bean.userinfo.TtMiniUserDataRequest;
import vip.gadfly.tiktok.mini.bean.userinfo.TtMiniVideoListResult;
import vip.gadfly.tiktok.open.bean.oauth2.TtOpAccessTokenRequest;
import vip.gadfly.tiktok.open.bean.userinfo.TtOpFansCheckResult;
import vip.gadfly.tiktok.open.bean.userinfo.TtOpFollowingListResult;
import vip.gadfly.tiktok.open.bean.userinfo.TtOpUserInfoResult;
import vip.gadfly.tiktok.open.common.ITtOpBaseService;

import static vip.gadfly.tiktok.core.enums.TtOpApiUrl.MiniUserInfo.*;

/**
 * @author Gadfly
 * @since 2021-09-30 14:42
 */
@Slf4j
@RequiredArgsConstructor
public class TtMiniUserInfoServiceImpl implements TtMiniUserInfoService {
    private final ITtOpBaseService ttOpBaseService;

    protected TtOpConfigStorage getTtOpConfigStorage() {
        return this.ttOpBaseService.getTtOpConfigStorage();
    }

    public static String code = "kjBnGTbPMy37_hlhhDkIuzbiPvWHA9ZA8oGcacGt_msVKkPiQLcFm-FzBfScoTYpBAPzGHS-Rh4aPm4jPfwHNHV7IC4lz5SBNSrneOOHbbrRI2ahFzr72edHpXg";


    /**
     * @param authorizationCode
     * @return
     */
    @Override
    public TtMiniAccessTokenResult getAccessTokenByAuthorizationCode(String authorizationCode) {
        log.debug("使用授权码换取用户信息的接口调用凭据，收到的参数：authorizationCode={}", authorizationCode);
        TtOpConfigStorage ttOpConfigStorage = getTtOpConfigStorage();
        String url = OAUTH2_GET_ACCESS_TOKEN_URL.getUrl(ttOpConfigStorage);
        log.debug("url={}", url);
        TtMiniAccessTokenRequest request = new TtMiniAccessTokenRequest()
                .setGrantType(TtOpAccessTokenRequest.GRANT_TYPE_CODE)
                .setClientKey(ttOpConfigStorage.getMiniClientKey())
                .setClientSecret(ttOpConfigStorage.getMiniClientSecret())
                .setCode(authorizationCode);

        Multimap<String, String> headers = LinkedListMultimap.create();
        headers.put("Content-Type", "application/json");
        TtMiniAccessTokenResult result = this.ttOpBaseService.postWithHeaders(url, headers, request, TtMiniAccessTokenResult.class);
        ttOpConfigStorage.updateAccessToken(result);
        ttOpConfigStorage.updateRefreshToken(result);
        return result;
    }


    @Override
    public TtMiniAccessTokenResult getMiniUserLogin(String code) {
        log.debug("使用授权码换取用户信息的接口调用凭据，收到的参数：code={}", code);
        TtOpConfigStorage ttOpConfigStorage = getTtOpConfigStorage();
        String url = OAUTH2_MINI_LOGIN_OPENID_URL.getUrl(ttOpConfigStorage);
        log.debug("url={}", url);
        TtMiniUserDataRequest request = new TtMiniUserDataRequest()
                .setAppid(ttOpConfigStorage.getMiniClientKey())
                .setSecret(ttOpConfigStorage.getMiniClientSecret())
                .setCode(code);
        Multimap<String, String> headers = LinkedListMultimap.create();
        headers.put("Content-Type", "application/json");
        TtMiniAccessTokenResult result = this.ttOpBaseService.postWithHeaders(url, headers, request, TtMiniAccessTokenResult.class);
        return result;
    }



    @Override
    public TtMiniVideoListResult getExternalList(String openId, Integer dateType) {
        log.debug("获取视频视频，收到的参数：openid={}, cursor={} ", openId, dateType);
        String url = String.format(
                GET_EXTERNAL_USER_URL.getUrl(getTtOpConfigStorage()),
                openId,
                dateType);

        Multimap<String, String> headers = LinkedListMultimap.create();
        headers.put("Content-Type", "application/json");
        headers.put("access-token",  this.ttOpBaseService.getAccessToken(openId));

        log.debug("url={}", url);
        return this.ttOpBaseService.getWithHeader(url, headers, TtMiniVideoListResult.class);
    }

    @Override
    public TtMiniFansListResult getFansList(String openId,  Integer dateType) {
        log.debug("获取粉丝列表，收到的参数：openid={}, cursor={}, dateType={}", openId, dateType);
        String url = String.format(
                GET_FANS_LIST_URL.getUrl(getTtOpConfigStorage()),
                openId,
                dateType);
        log.debug("url={}", url);

        Multimap<String, String> headers = LinkedListMultimap.create();
        headers.put("Content-Type", "application/json");
        headers.put("access-token",  this.ttOpBaseService.getAccessToken(openId));

        return this.ttOpBaseService.getWithHeader(url, headers, TtMiniFansListResult.class);
    }

    @Override
    public TtOpFollowingListResult getFollowingList(String openId, Long cursor, Integer count) {
        log.debug("获取关注列表，收到的参数：openid={}, cursor={}, count={}", openId, cursor, count);
        String url = String.format(
                GET_FOLLOWING_LIST_URL.getUrl(getTtOpConfigStorage()),
                openId,
                cursor,
                count,
                this.ttOpBaseService.getAccessToken(openId));
        log.debug("url={}", url);
        return this.ttOpBaseService.get(url, TtOpFollowingListResult.class);
    }

    @Override
    public TtOpFansCheckResult getFansCheck(String openId, String followerOpenId) {
        log.debug("粉丝判断，收到的参数：openid={}, followerOpenId={}", openId, followerOpenId);
        String url = String.format(
                GET_FANS_CHECK_URL.getUrl(getTtOpConfigStorage()),
                openId,
                followerOpenId,
                this.ttOpBaseService.getAccessToken(openId));
        log.debug("url={}", url);
        return this.ttOpBaseService.get(url, TtOpFansCheckResult.class);
    }
}
